package limousine.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts.upload.FormFile;

/**
 * 
 * @author TAI
 * 
 */
public class Dir {

    private String prefixPath = "";
//    private String[] sepRootFolder = {"var", "HYDRA"};
    private String tmpFolder = "TMP";
    private String deployFolder = "DEPLOY";
    private String contentFolder = "CONTENT";
    private String transformFolder = "TRANSFORM";
    private String systemFolder = "SYSTEMS";

    public Dir() {
        String osName = System.getProperty("os.name");
//        System.out.println("OS [" + osName + "]");
        if (osName.indexOf("Windows") != -1) {//window
            prefixPath = "D:";//on window
        }        
//        System.out.println("prefixPath [" + prefixPath + "]");
    }

    public String getTransformFolder() {
        return transformFolder;
    }

    public void setTransformFolder(String transformFolder) {
        this.transformFolder = transformFolder;
    }

    public String getContentFolder() {
        return contentFolder;
    }

    public void setContentFolder(String contentFolder) {
        this.contentFolder = contentFolder;
    }

    public String getSystemFolder() {
        return systemFolder;
    }

    public void setSystemFolder(String systemFolder) {
        this.systemFolder = systemFolder;
    }

    public String getFileSeperator() {
        return File.separator;
    }

    public void setPrefixPath(String prefixPath) {
        this.prefixPath = prefixPath;
    }

    public String getPrefixPath() {
        return this.prefixPath;
    }

    public String getDeployFolder() {
        return deployFolder;
    }

    public void setDeployFolder(String deployFolder) {
        this.deployFolder = deployFolder;
    }

    public String getTmpFolder() {
        return tmpFolder;
    }

    public void setTmpFolder(String tmpFolder) {
        this.tmpFolder = tmpFolder;
    }

    public boolean createFolder(String name) {
        return (new File(this.prefixPath + name)).mkdir();
    }

    public boolean createSubFolder(String[] name) {
        String path = this.prefixPath;
        for (int i = 0; i < name.length; i++) {
            path += name[i];
            if (!(new File(path)).mkdir()) {// if can't create
                return false;
            }
            path += File.separator;
        }
        return true;
    }

    public boolean renameFile(String fileName, String newName) {
        //	System.out.println("3 "+this.rootFolder+fileName);
        //	System.out.println("3 "+this.rootFolder+newName);
        File f = new File(this.prefixPath + fileName);
        return f.renameTo(new File(this.prefixPath + newName));
    }

    public boolean copyFile(String srcFile, String targetFile) {
        try {
            File f1 = new File(this.prefixPath + srcFile);
            File f2 = new File(this.prefixPath + targetFile);
            InputStream in = new FileInputStream(f1);
            OutputStream out = new FileOutputStream(f2);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            return true;
        } catch (FileNotFoundException ex) {
            System.out.println("copyFile " + ex.toString());
        } catch (IOException e) {
            System.out.println("copyFile " + e.toString());
        }
        return false;
    }

    public boolean deleteFile(String file) {
        File f1 = new File(this.prefixPath + file);
        if (f1.exists()) {
            // if have will delete
            boolean success = f1.delete();
            if (!success) {
                System.out.println("deleteFile fail:[" + file + "]");
                return false;
            } else {
                System.out.println("deleted file:[" + file + "]");
                return true;
            }
        } else {
            System.out.println("Not found:[" + file + "] for delete");
            return false;
        }
    }

    public boolean checkHaveFolder(String name) {
        File f1 = new File(this.prefixPath + name);
        if (f1.exists()) {
            return true;
        }
        return false;
    }

    public boolean deleteFolder(String folderPath) {
        System.out.println("deleteFolder:"+folderPath);
        File dir = new File(this.prefixPath + folderPath);
        File fs[] = dir.listFiles();
        if (fs != null) {//have folder or file
            for (int i = 0; i < fs.length; i++) {
                if (fs[i].isDirectory()) {
                    deleteFolder(folderPath + File.separator + fs[i].getName());
                } else {
                    deleteFile(folderPath + File.separator + fs[i].getName());
                }
            }
            dir.delete();
            System.out.println("Delete:"+folderPath);
        }
        return true;
    }

    public boolean clearFolder(String folderPath) {
        File dir = new File(this.prefixPath + folderPath);
        File fs[] = dir.listFiles();
        if (fs != null) {//have folder or file
            for (int i = 0; i < fs.length; i++) {
                if (fs[i].isDirectory()) {
                    clearFolder(folderPath + File.separator + fs[i].getName());
                } else {
                    deleteFile(folderPath + File.separator + fs[i].getName());
                }
            }
        //dir.delete();
        }
        return true;
    }

    public boolean writeFile(FormFile file, String target) {
        FileOutputStream out;
        try {
            out = new FileOutputStream(this.prefixPath+target);
            byte[] b = new byte[1024];
            int count = 0;
            InputStream in = file.getInputStream();
            while ((count = in.read(b)) >= 0) {
                out.write(b, 0, count);
            }
            out.close();
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }
    public boolean moveFile(String srcFile, String targetFile) {
        try {
            File f1 = new File(this.prefixPath+srcFile);
            File f2 = new File(this.prefixPath+targetFile);
            InputStream in = new FileInputStream(f1);
            OutputStream out = new FileOutputStream(f2);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

            deleteFile(srcFile);
            return true;
        } catch (FileNotFoundException ex) {
            System.out.println("moveFile " + ex.toString());
        } catch (IOException e) {
            System.out.println("moveFile " + e.toString());
        }
        return false;
    }
}
